package com.jiayinlending.retrofit;

import android.content.Context;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.jiayinlending.AppContext;
import com.jiayinlending.retrofit.api.HomeApi;
import com.jiayinlending.retrofit.api.MineApi;
import com.jiayinlending.retrofit.api.RepaymentApi;

import java.io.File;

import okhttp3.Cache;
import okhttp3.OkHttpClient;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava.RxJavaCallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;
import rx.Scheduler;
import rx.schedulers.Schedulers;

/**
 * Created by yuexiaohui on 2017/5/5 16:21
 */

public class JiaYinDaiRetrofit {

    private static final String BASE_URL = "http://192.168.1.156:8080";

    private Context context = AppContext.context();

    final static Gson gson = new GsonBuilder()
            .setDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'")
            .serializeNulls()
            .create();
    final HomeApi homeApi;
    final MineApi mineApi;
    final RepaymentApi repaymentApi;

    public JiaYinDaiRetrofit() {
        OkHttpClient.Builder okHttpClientBuilder = createDefaultOkHttpClientBuilder(context);
        if (JiaYinDaiFactory.isDebug) {
            HttpLoggingInterceptor logging = new HttpLoggingInterceptor();
            logging.setLevel(HttpLoggingInterceptor.Level.BODY);
            okHttpClientBuilder.addInterceptor(logging);
        }
        OkHttpClient client = okHttpClientBuilder.build();
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(BASE_URL)
                .client(client)
                .addConverterFactory(createGsonConverterFactory(gson))
                .addCallAdapterFactory(createRxJavaCallAdapterFactory(Schedulers.io()))
                .build();

        homeApi = retrofit.create(HomeApi.class);
        mineApi = retrofit.create(MineApi.class);
        repaymentApi = retrofit.create(RepaymentApi.class);
    }

    public HomeApi getHomeApi() {
        return homeApi;
    }

    public MineApi getMineApi() {
        return mineApi;
    }

    public RepaymentApi getRepaymentApi() {
        return repaymentApi;
    }


    public static Cache createDefaultCache(Context context) {
        File cacheDir = new File(context.getCacheDir().getAbsolutePath(), "/okhttp-jiayindai/");
        if (cacheDir.mkdirs() || cacheDir.isDirectory()) {
            return new Cache(cacheDir, 1024 * 1024 * 10);
        }
        return null;
    }

    private GsonConverterFactory createGsonConverterFactory(Gson gson) {
        return GsonConverterFactory.create(gson);
    }

    private RxJavaCallAdapterFactory createRxJavaCallAdapterFactory(Scheduler scheduler) {
        return RxJavaCallAdapterFactory.createWithScheduler(scheduler);
    }

    public static OkHttpClient.Builder createDefaultOkHttpClientBuilder(Context context) {
        return new OkHttpClient.Builder();
//                .cache(createDefaultCache(context));
    }

}
